package com.f5.management.controller;

import com.f5.management.entity.Community;
import com.f5.management.service.CommunityService;
import com.f5.management.utils.PageResult;
import com.f5.management.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/community")
@CrossOrigin(origins = {"http://localhost:8080", "null"})
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    //添加信息
    @RequestMapping("/add")
    public ResponseResult<String> add(Community community) {

        return new ResponseResult<String>(communityService.add(community));
    }

    //通过小区名字查询cid
    @RequestMapping("/findCidByName")
    public int findCidByName(String name) {

        return communityService.findCidByName(name);
    }

    //id查询数据进行修改
    @RequestMapping("/findById")
    public ResponseResult<Community> findById(int id) {
        Community community = communityService.findById(id);
        return new ResponseResult<Community>(true).setData(community);
    }

    //更改信息
    @RequestMapping("/updateInfo")
    public ResponseResult<String> updateInfo(Community community) {

        return new ResponseResult<String>(communityService.updateInfo(community));

    }

    //查询编号是否存在
    @RequestMapping("/selectNumber")
    public ResponseResult<String> selectNumber(String serialNumber) {
        return new ResponseResult<String>(communityService.selectNumber(serialNumber));
    }

    //删除信息
    @RequestMapping("/delInfo")
    public ResponseResult<String> delInfo(int id) {

        return new ResponseResult<String>(communityService.delInfo(id));

    }

    //更改状态
    @RequestMapping("/updateStatus")
    public ResponseResult<String> updateStatus(int id, boolean status) {

        return new ResponseResult<String>(communityService.updateStatus(id, status));

    }

    //分页展示信息
    @RequestMapping("/findByPage")
    public PageResult<List<Community>> findByPage(int page, int size) {

        return communityService.findByPage(page, size);
    }

    //通过名字查找信息
    @RequestMapping("/findByName")
    public PageResult<List<Community>> findByName(String serialNumber, String name, int page) {
        //调用service方法查询数据
        PageResult<List<Community>> community = communityService.findByName(serialNumber, name, page, 10);
        return community;
    }

    //图片上传
//    @Value("${images.location}")
//    private String path;
//
//    @PostMapping("/upload")
//    public ResponseResult<String> upload(MultipartFile image) {
//        UUID uuid = UUID.randomUUID();
//        String fileName = uuid.toString().replaceAll("-", "");
//        try {
//            image.transferTo(new File(path + "/" + fileName));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return new ResponseResult<String>(true).setData(fileName);
//    }

}

















